package com.example.node;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.node.fragment.AppFragment;
import com.example.node.fragment.HomeFragment;
import com.example.node.fragment.SystemFragment;
import com.example.node.fragment.TimeFragment;
import com.example.node.fragment.WallpaperFragment;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private Toolbar mToolbar;
    private NavigationView main_nav_view;
    private DrawerLayout main_drawer_layout;

    private HomeFragment homeFragment;
    private WallpaperFragment wallpaperFragment;
    private AppFragment appFragment;
    private SystemFragment systemFragment;
    private TimeFragment timeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initToolBar();
        initView();
        initHeaderView();
    }

    private void initHeaderView() {
        View view=main_nav_view.getHeaderView(0);
        ImageView iv_header_photo=view.findViewById(R.id.iv_header_photo);
        TextView tv_header_title=view.findViewById(R.id.tv_header_title);
        tv_header_title.setText("小鹿男");
        TextView tv_header_content=view.findViewById(R.id.tv_header_content);
        tv_header_content.setText("为中华之崛起而读书!");
    }

    private void initToolBar() {
        mToolbar=$(R.id.mToolbar);
        setSupportActionBar(mToolbar);
    }

    private void initView() {
        main_nav_view=$(R.id.main_nav_view);
        //设置监听
        main_nav_view.setNavigationItemSelectedListener(this);
        //去掉图标icon渲染
        main_nav_view.setItemIconTintList(null);
        main_drawer_layout=$(R.id.main_drawer_layout);
        //显示和隐藏侧边栏按钮
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,main_drawer_layout,mToolbar,R.string.openDrawerContentDescRes,R.string.closeDrawerContentDescRes);
        //切换菜单的动画
        main_drawer_layout.addDrawerListener(toggle);
        //同步状态
        toggle.syncState();
        //显示默认的Fragment
        showHomeFragment();
    }

    public <T> T $(int viewID){
        return (T) findViewById(viewID);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.nav_menu_home:
                showHomeFragment();
                break;

            case R.id.nav_menu_wallpaper:
                showWallpaperFragment();
                break;

            case R.id.nav_menu_app:
                showAppFragment();
                break;

            case R.id.nav_menu_system:
                showSystemFragment();
                break;
            case R.id.nav_menu_time:
                showTimeFragment();
                break;
            case R.id.nav_menu_share:

                break;
            case R.id.nav_menu_setting:

                break;
        }
        //恢复
        main_drawer_layout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_sao:

                break;

            case R.id.menu_http:

                break;

            case R.id.menu_share:

                break;
            case R.id.menu_setting:

                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //切换显示HomeFragment()
    private void showHomeFragment(){
        FragmentManager fm= getSupportFragmentManager();
        FragmentTransaction ft= fm.beginTransaction();
        if(homeFragment==null){
            homeFragment=new HomeFragment();
            ft.add(R.id.main_content_layout,homeFragment);
        }
        hideAllFragment(ft);
        ft.show(homeFragment);
        ft.commit();
    }
    //切换显示WallpaperFragment()
    private void showWallpaperFragment(){
        FragmentManager fm= getSupportFragmentManager();
        FragmentTransaction ft= fm.beginTransaction();
        if(wallpaperFragment==null){
            wallpaperFragment=new WallpaperFragment();
            ft.add(R.id.main_content_layout,wallpaperFragment);
        }
        hideAllFragment(ft);
        ft.show(wallpaperFragment);
        ft.commit();
    }
    //切换显示AppFragment()
    private void showAppFragment(){
        FragmentManager fm= getSupportFragmentManager();
        FragmentTransaction ft= fm.beginTransaction();
        if(appFragment==null){
            appFragment=new AppFragment();
            ft.add(R.id.main_content_layout,appFragment);
        }
        hideAllFragment(ft);
        ft.show(appFragment);
        ft.commit();
    }
    //切换显示SystemFragment()
    private void showSystemFragment(){
        FragmentManager fm= getSupportFragmentManager();
        FragmentTransaction ft= fm.beginTransaction();
        if(systemFragment==null){
            systemFragment=new SystemFragment();
            ft.add(R.id.main_content_layout,systemFragment);
        }
        hideAllFragment(ft);
        ft.show(systemFragment);
        ft.commit();
    }
    private void showTimeFragment(){
        FragmentManager fm= getSupportFragmentManager();
        FragmentTransaction ft= fm.beginTransaction();
        if(timeFragment==null){
            timeFragment=new TimeFragment();
            ft.add(R.id.main_content_layout,timeFragment);
        }
        hideAllFragment(ft);
        ft.show(timeFragment);
        ft.commit();
    }

    //隐藏所有的Frahment
    private void hideAllFragment(FragmentTransaction ft){
        if(homeFragment!=null){
            ft.hide(homeFragment);
        }
        if(wallpaperFragment!=null){
            ft.hide(wallpaperFragment);
        }
        if(appFragment!=null){
            ft.hide(appFragment);
        }
        if(systemFragment!=null){
            ft.hide(systemFragment);
        }
        if(timeFragment!=null){
            ft.hide(timeFragment);
        }
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        if (homeFragment==null&&fragment instanceof HomeFragment){
            homeFragment= (HomeFragment) fragment;
        }else if (wallpaperFragment==null&&fragment instanceof WallpaperFragment){
            wallpaperFragment= (WallpaperFragment) fragment;
        }else if (appFragment==null&&fragment instanceof AppFragment){
            appFragment= (AppFragment) fragment;
        }else if (systemFragment==null&&fragment instanceof SystemFragment){
            systemFragment= (SystemFragment) fragment;
        } else if (timeFragment==null&&fragment instanceof TimeFragment){
            timeFragment= (TimeFragment) fragment;
        }
    }
}
